<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        :addPer="'SystemUser:create'"
        :selectPer="'SystemUser:page'"
        addTitle="新增员工"
        @insert="add"
        @search="search"
        :showSearchButton="true"
        :showAddButton="true"
      >
        <job-shop v-model="form.storeId" placeholder="任职门店" />
        <el-select
          clearable
          style="width: 200px"
          v-model="form.status"
          placeholder="全部状态"
        >
          <el-option
            v-for="item in dict['状态']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <el-input
          v-model="form.userName"
          clearable
          placeholder="员工姓名"
          style="width: 200px"
        />
        <el-input
          v-model="form.phone"
          clearable
          placeholder="联系方式"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="编辑"
            style="cursor: pointer"
            v-permission="'SystemUser:update'"
            @click="updateUser(row)"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="'SystemUser:delete'"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import {
  userPage,
  changeStatus,
  deleteUser,
  addUser,
} from '@/api/after-sale/personnel/index'
import { mapGetters } from 'vuex'
import { getDict } from '@/utils/index'
export default {
  mixins: [
    baseMixin({
      idName: 'id',
      findData: userPage,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.statusName = data.status == true ? '启用' : '禁用'
          return data
        })
      },
      deleteData: deleteUser,
      // addData: addUser,
      // findDataById: getDictId,
      // updateData: updateDict,
    }),
  ],

  data() {
    return {
      form: {
        phone: '', //电话
        status: '', //状态
        storeId: '', //门店id
        userName: '', //用户名
      },
      tableColumnAttributes: [
        {
          name: '任职门店',
          value: 'storeName',
          type: 'text',
        },
        {
          name: '员工编号',
          value: 'code',
          type: 'text',
        },
        {
          name: '登录名',
          value: 'account',
          type: 'text',
        },
        {
          name: '姓名',
          value: 'userName',
          type: 'text',
        },
        {
          name: '职位',
          value: 'roleName',
          type: 'text',
        },
        {
          name: '联系方式',
          value: 'phone',
          type: 'text',
        },
        {
          name: '状态',
          value: 'statusName',
          type: 'switch',
          valueId: 'status',
          action: (row) => {
            changeStatus(row.id).then((res) => {
              this.search()
            })
          },
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'text',
        },
        {
          name: '最后登录时间',
          value: 'loginTime',
          type: 'text',
        },
      ],
      dict: {},
    }
  },
  methods: {
    //编辑员工信息
    updateUser(row) {
      let { id } = row
      this.$router.push({
        path: '/personnel/updataEmployee',
        query: {
          userId: id,
        },
      })
    },
    // 查询
    search() {
      this.searchOptions = this.form
      this.currentPage = 1
      this.setTableData()
    },
    add() {
      this.$router.push({
        path: '/personnel/addEmployee'
      })
    },
  },
  mounted() {
    this.dict = getDict(['状态'])
  },
}
</script>
